#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket() and bind() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_ntoa() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */

#define ECHOMAX 255     /* Longest string to echo */

void DieWithError(const char *errorMessage) /* External error handling function */
{
    perror(errorMessage);
    exit(1);
}

int main(int argc, char *argv[])
{
    int sock;                        /* Socket */
    struct sockaddr_in echoServAddr; /* Local address */
    struct sockaddr_in echoClntAddr; /* Client address */
    unsigned int cliAddrLen;         /* Length of incoming message */
    char echoBuffer[ECHOMAX];        /* Buffer for echo string */
    unsigned short echoServPort;     /* Server port */
    int recvMsgSize;                 /* Size of received message */
    char message[5]= ""; 
    char ch;
    int r = -1;
    int R;
    char temp[23];
    char clientip[15];
    char inc[2];
    char clientn[2];
    char reqnum[2];
    char letter[1];
    int i;

    if (argc != 2)         /* Test for correct number of parameters */
    {
        fprintf(stderr,"Usage:  %s <UDP SERVER PORT>\n", argv[0]);
        exit(1);
    }

    echoServPort = atoi(argv[1]);  /* First arg:  local port */

    /* Create socket for sending/receiving datagrams */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        DieWithError("socket() failed");

    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons(echoServPort);      /* Local port */

    /* Bind to the local address */
    if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("bind() failed");
  
    for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        cliAddrLen = sizeof(echoClntAddr);

        /* Block until receive message from a client */
        if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0,
            (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0)
            DieWithError("recvfrom() failed");

        printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
   
	for (i=0; i<22; i++)
	{
		temp[i] = echoBuffer[i];
	}

	for (i=0; i<15; i++)
	{
		clientip[i] = temp[i];
	}


	for (i=15; i<17; i++)
	{
		inc[i-15] = temp[i];	
	}

	printf("client ip: %s\n", clientip);
	printf("incarnation: %s\n", inc);

/*        srand ( time(NULL))
        int randnum = rand() % (10-1+1)+1;
        // printf("%d\n", r);
        // char c = (char)r;
        // printf("%c\n", c);
        // sleep(1);
      
	if (randnum == 1)
	{
		;
	}
	else if (randnum == 2)
	{
		;
	}
	else 
	{
		;
	}

        if (R<r)
	{
		;
	}
	else if (R == r)
	{
		;
	}
	else if (R>r) 
	{
	    message[0] = ch;
	}*/

        /* Send received datagram back to the client */
        if (sendto(sock, echoBuffer, recvMsgSize, 0, 
             (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize)
            DieWithError("sendto() sent a different number of bytes than expected");
    }
    /* NOT REACHED */

    struct client {
	    char processor[16];
	    int incar;
	    int clientNum;
	    char response[5];
    };

    struct client table[40];
}
